package cn.crm.service;

import cn.crm.entiy.Customer;
import cn.crm.entiy.Record;
import cn.crm.entiy.User;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class CustomerServiceImpl implements CustomerService{

    private List<Customer> customerList;

    public CustomerServiceImpl() {
        this.customerList = new ArrayList<Customer>(){{
            add(new Customer("华为","88888888","张三"));
            add(new Customer("阿里","88888889","张三"));
            add(new Customer("腾讯","88888880","李四"));
        }};
    }

    @Override
    public void showList(User user) {
        //客户编号，公司名称，电话， 联系人
        System.out.println("客户编号\t\t公司名称\t\t电话\t\t联系人");
        this.customerList.forEach(c->{
            if(user.getName().equals(c.getManager())){
                System.out.println(c.getId()+"\t\t"
                        +c.getCompany()+"\t\t"
                        +c.getPhone()+"\t\t"
                        +c.getManager()
                );
            }

        });
    }

    @Override
    public void add(Customer customer) {
        this.customerList.add(customer);
    }

    @Override
    public void update(Customer customer, User user) {
        customer.setManager(user.getName());
    }

    @Override
    public void addRecord(Customer customer, String record) {
        //创建记录
        Record record1 = new Record(new Date(), record);
        //将记录放入列表
        customer.getRecordList().add(record1);
    }

    @Override
    public void showRecordList(Customer customer) {
        //打印公司信息
        System.out.println(customer.getId()+"\t\t"
                +customer.getCompany()+"\t\t"
                +customer.getPhone()+"\t\t"
                +customer.getManager()
        );
        //打印记录列表
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        customer.getRecordList().forEach(r->{
            System.out.println("录入时间："+sdf.format(r.getTime()));
            System.out.println("访问内容："+r.getAbout());
        });
    }

    @Override
    public Customer getCustomer(int id) {
        for (Customer cus:
             customerList) {
            if(cus.getId()==id){
                return cus;
            }
        }
        System.out.println("输入有误");
        return null;
    }
}
